package org.xp.judgeplatform.util;

import java.security.SecureRandom;

/**
 * 邀请码生成器
 * TEMP-前缀 + 8位随机码
 */
public class InviteCodeGenerator {

    private static final String TEMP_CHARS  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    private static final int TEMP_CODE_LENGTH = 8;
    private static final String PREFIX = "TEMP-";
    private static final SecureRandom random = new SecureRandom();

    /**
     * 生成随机8位邀请码
     */
    public static String generate() {
        String tempCode = random.ints(0, TEMP_CHARS.length())
                .limit(TEMP_CODE_LENGTH)
                .mapToObj(TEMP_CHARS::charAt)
                .collect(StringBuilder::new, StringBuilder::append, StringBuilder::append)
                .toString();
        return PREFIX + tempCode;
    }

    /**
     * 验证邀请码
     */
    public static boolean isTempCode(String tempCode) {
        return tempCode.startsWith(PREFIX) && tempCode.length() == PREFIX.length() + TEMP_CODE_LENGTH;
    }
}
